<template>
  <el-row>
    <el-col :span="24">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" label-position="right" size="mini">
        <el-form-item label="执行人类型" prop="performerType">
          <el-select v-model="form.performerType" clearable>
            <el-option v-for="item in performTypes" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="useExpression" label="是否使用表达式">
          <el-radio-group v-model="form.useExpression">
            <el-radio label="true" border>是</el-radio>
            <el-radio label="false" border>否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form.useExpression=='true'" prop="expression" label="表达式">
          <el-input v-model="form.expression" clearable></el-input>
        </el-form-item>
        <el-form-item prop="formKey" label="表单Key">
          <el-input v-model="form.formKey" clearable></el-input>
        </el-form-item>
        <el-form-item prop="dueDate" label="过期时间">
          <el-input v-model="form.dueDate" clearable></el-input>
        </el-form-item>
        <el-form-item prop="priority" label="优先级">
          <el-input v-model="form.priority" clearable></el-input>
        </el-form-item>
        <el-form-item prop="documentation" label="说明">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" :rows="3" v-model="form.documentation" clearable></el-input>
        </el-form-item>
      </el-form>
    </el-col>
    <el-col :span="24" v-if="form.useExpression==='false'">
      <candidate-users v-if="form.performerType==='candidateUsers'"></candidate-users>
      <candidate-groups v-if="form.performerType==='candidateGroups'"></candidate-groups>
    </el-col>
    <el-col :span="24">
      <el-row type="flex" justify="center">
        <el-col :span="2">
          <el-button type="primary" icon="el-icon-edit" @click="submitForm('form')">保存</el-button>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { mapState, mapActions, mapGetters } from "vuex";
import Message from "../../common/Message";
import CandidateUsers from "./CandidateUsers";
import CandidateGroups from "./CandidateGroups";
export default {
  components: {
    CandidateUsers,
    CandidateGroups
  },
  data() {
    return {
      form: {
        performerType: "",
        useExpression: "",
        expression: "",
        formKey: "",
        dueDate: "",
        priority: "",
        documentation: ""
      },
      performTypes: [
        {
          value: "candidateUsers",
          label: "用户"
        },
        {
          value: "candidateGroups",
          label: "用户组"
        }
      ],
      rules: {
        id: [
          {
            required: true,
            message: "请输入id",
            trigger: "blur"
          }
        ],
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    ...mapActions([
      "saveTaskPerformer",
      "saveCandidateGroups",
      "saveCandidateUsers"
    ]),
    loadPerformer() {
      let task = this.$store.state.selectedTask;
      if (task != null && task != undefined) {
        this.form.performerType = task.performerType;
        this.form.expression = task.expression;
        this.form.useExpression = task.isUseExpression;
        this.form.formKey = task.formKey;
        this.form.dueDate = task.dueDate;
        this.form.priority = task.priority;
        this.form.documentation = task.documentation;
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveTaskPerformer(this.form);
          this.saveCandidateGroups();
          this.saveCandidateUsers();
          Message.success("保存成功！");
        } else {
          return false;
        }
      });
    }
  },
  mounted() {
    this.loadPerformer();
  }
};
</script>
